import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/6/8
 * Time: 21:04
 */

public class Solution {
    public static String decodeString(String s) {
        if (s == null || s.isEmpty()) {
            return "";
        }
        Stack<Integer> numStack = new Stack<>();
        Stack<String> stringStack = new Stack<>();
        int num = 0;
        String curString = "";
        for (int i = 0; i < s.length(); i++) {
            //获取当前字符
            char cur = s.charAt(i);
            //如果是数字
            if(Character.isDigit(cur)){
                num = num * 10 + cur - '0';
            }else if(cur == '['){
                numStack.push(num);
                stringStack.push(curString);
                num = 0;
                curString = "";
            }else if(cur == ']'){
                int time = numStack.pop();
                StringBuilder builder = new StringBuilder(stringStack.pop());
                for(int j = 0; j < time; j++){
                    builder.append(curString);
                }
                curString = builder.toString();
            }else {
                curString += cur;
            }
        }
        return curString;
    }
    public static void main(String[] args) {
        String s = decodeString("3[a]2[bc]");
        System.out.println(s);
    }
}